//
//  FWAttentUserController.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/10.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWAttentUserController.h"
#import "FWAttentService.h"
#import "FWAttentUserCell.h"
#import "FWTypeModel.h"
#import <MJRefresh.h>

@interface FWAttentUserController ()
@property (weak, nonatomic) MJRefreshBackStateFooter *footer;
@property (strong, nonatomic) MJRefreshNormalHeader *header;
@property (strong, nonatomic) FWUserRequest *request;
@end

@implementation FWAttentUserController
static NSString * const ID = @"FWAttentUserCell";

-(void)setModel:(FWTypeModel *)model{
    _model = model;
    [self getUserData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    [self addRefrsh];
}

- (void)addRefrsh{
    MJRefreshBackStateFooter *footer = [MJRefreshBackStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    [footer setTitle:@"松手加载更多" forState:MJRefreshStatePulling];
    [footer setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"向上滑动，加载更多" forState:MJRefreshStateIdle];
    footer.stateLabel.textColor = MyColor(118, 132, 155);
    self.tableView.mj_footer = footer;
    _footer = footer;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshNewData)];
    self.tableView.mj_header = header;
    _header = header;
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellReuseIdentifier:ID];
    self.tableView.rowHeight = 60;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
}


- (void)getUserData{
//    [_footer endRefreshing];
    [_header endRefreshing];
    if (_model.users.count) {//避免重复请求
        [self.tableView reloadData];
    }else{
        [self.tableView reloadData];
        [_header beginRefreshing];
    }
}

- (void)checkFooterState{
    
    // 每次刷新右边数据时, 都控制footer显示或者隐藏
    _footer.hidden = (_model.users.count == 0);
    
    // 让底部控件结束刷新
    if (_model.users.count >= _model.count) { // 全部数据已经加载完毕
        _footer.hidden = YES;
    } else { // 还没有加载完毕
        [_footer endRefreshing];
        _footer.hidden = NO;
    }
}

- (void)refreshNewData{
    _model.page = 1;
    FWUserRequest *request = [[FWUserRequest alloc] init];
    request.a = @"list";
    request.c = @"subscribe";
    request.category_id = _model.ID;
    request.page = _model.page;
    _request = request;
    [FWAttentService getUserDataWith:request success:^(FWUserResponse *response) {
       
        [_model.users removeAllObjects];
        [_model.users addObjectsFromArray:response.list];
        //如果不是最新的请求不筛选
        if (_request != request) return;
       
        [self.tableView reloadData];
        [_header endRefreshing];
        [self checkFooterState];
    } failure:^(NSError *error) {
        if (_request != request) return;
        [_header endRefreshing];
    }];
}

- (void)loadMore{
    FWUserRequest *request = [[FWUserRequest alloc] init];
    request.a = @"list";
    request.c = @"subscribe";
    request.category_id = _model.ID;
    request.page = ++(_model.page);
    _request = request;
    
    [FWAttentService getUserDataWith:request success:^(FWUserResponse *response) {
        
        _model.page = request.page;
        [_model.users addObjectsFromArray:response.list];
        //如果不是最新的请求不筛选
        if (_request != request) return;
        
        [self.tableView reloadData];
        
        [self checkFooterState];
        
    } failure:^(NSError *error) {
        if (_request != request) return;
        [_footer endRefreshing];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [self checkFooterState];
    return _model.users.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FWAttentUserCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.model = _model.users[indexPath.row];
    return cell;
}

- (void)dealloc{
    _request = nil;
    [FWHttpUtil cancelManager];
}

@end
